package com.neusoft.news.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.neusoft.news.bean.User;
import com.neusoft.news.service.UserService;


/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String level = request.getParameter("level");
		
		User user = new User();
		List<User>users = new ArrayList<User>();
		
		user.setUserName(username);
		user.setUserPwd(password);
		user.setUserLevel(level);
		if("1".equals(level)){
			UserService service = new UserService();
			User user1;
			try {
				user1 = service.login(user);
				if(user1.getUserId() != 0) {
					//将数据存储到session中
					HttpSession session = request.getSession();
			        session.setAttribute("user", user1);
					request.getRequestDispatcher("ShowNewsListServlet").forward(request, response);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				request.getRequestDispatcher("error2.jsp").forward(request, response);
			}
		} else if("2".equals(level)){
			UserService service = new UserService();
			User user1;
			try {
				user1 = service.login(user);
				if(user1.getUserId() != 0) {
					//将数据存储到session中
					HttpSession session = request.getSession();
			        session.setAttribute("user", user1);
					request.getRequestDispatcher("ShowNewsListServlet").forward(request, response);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				request.getRequestDispatcher("error2.jsp").forward(request, response);
			}
			
		}else if("3".equals(level)){
			UserService service = new UserService();	
				try {
					User user1 = service.login(user);
					if(user1.getUserId() != 0) {
						users = service.selectNewUser();
						request.setAttribute("user",user1);
						request.setAttribute("users",users);
						request.getRequestDispatcher("userindex.jsp").forward(request, response);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					request.getRequestDispatcher("error2.jsp").forward(request, response);
				}
			
		}else {
			//登录异常画面
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
		
	}

}
